<?php
namespace Kernel;

class Temple
{
    public $assign;

    function assign($name,$val)
    {
        $this->assign[$name] = $val;
    }


    function display($filename)
    {
        $filenamePath = VIEW.'/'.$filename;
        if(!file_exists($filenamePath))
            throw new \Error('没有视图文件'.$filenamePath);
        $file = file_get_contents($filenamePath);
        if($this->assign == [])
            return $file;
        foreach ($this->assign as $key=>$val)
            $res = preg_replace("/@$key@/",$val,$file);
        return $res;
    }

    function response($html){
        $kernel = new Kernel();
        //返回中间件 从下至上
        foreach (array_reverse($kernel->middleware) as $cls){
            $obj = new $cls();
            $html = $obj->response($html);
        }
        echo $html;
        exit;
    }

    function redirect($url){
        header("Location: $url");
        exit();
    }
}